package com.helloae.chap06;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.geodatabase.FeatureClassName;
import com.esri.arcgis.geodatabase.FeatureDataConverter;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 新建一个Personal Geodatabase后，将一个shp文件插入其中。<br>
 * 等同于先用ArcCatalog创建新的Personal Geodatabase，再用Import/FeatureClass(single)导入数据<br>
 * 示例章节：6.3.1
 * @author zhushijie
 *
 */
public class ShapefileToAccess {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//创建用于输出的IFeatureClassName并配置其所需的参数
		WorkspaceName pWorkspaceName = new WorkspaceName();
		pWorkspaceName.setWorkspaceFactoryProgID("esriDataSourcesGDB.AccessWorkspaceFactory");
		pWorkspaceName.setPathName("c:\\temp\\Trial.mdb");
		FeatureClassName pFeatureClassName = new FeatureClassName();
		pFeatureClassName.setWorkspaceNameByRef(pWorkspaceName);
		//在Personal Geodatabase中的图层名为soils2
		pFeatureClassName.setName("soils2");
		
		//创建用于输入的IFeatureClassName并配置其所需的参数
		WorkspaceName pInShpWorkspaceName = new WorkspaceName();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		pInShpWorkspaceName.setPathName(ecliseWorkspace+"HelloAE"+File.separator+"DataSet"+File.separator+"chap06");
		pInShpWorkspaceName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
		FeatureClassName pShpDatasetName = new FeatureClassName();
		pShpDatasetName.setWorkspaceNameByRef(pInShpWorkspaceName);
		pShpDatasetName.setName("soils");
		
		//进行格式转换，即插入Personal Geodatabase，此过程会创建Personal Geodatabase再执行插入
		//如果Personal Geodatabase已存在则直接插入，如果库内已有同名文件则报错
		FeatureDataConverter pShpToFC = new FeatureDataConverter();
		pShpToFC.convertFeatureClass(pShpDatasetName, null, null, pFeatureClassName, null, null, "", 1000, 0);
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
